﻿using IntelligentStorWpfApp.Interface;
using IntelligentStorWpfApp.Services;
using IntelligentStorWpfApp.Model;
using log4net;
using System;
using IntelligentStorWpfApp.Helper;

namespace IntelligentStorWpfApp.Controller
{
    public class StatusController
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(LinkProductPositionController));
        StatusInterface statusInterface = new StatusServices();
        LinkProductPositionInterface linkPp = new LinkProductPositionServices();
        LinkProductPositionController lppc = new LinkProductPositionController();

        public bool UpdateStatus(Status status,string productId=null)
        {
            try
            {
                if (statusInterface.UpdateStatus(status))
                {
                    if (status.StoreStatus > 0)
                    {
                        linkPp.linkProductPosition(productId, status.PositionId);
                        LogHelper.AddLog("产品 " + productId + " 入库 ");
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(productId))
                        {
                            LinkProductPosition linkpposition = lppc.getLinkByPositionId(status.PositionId);
                            productId = linkpposition.ProductId;
                        }
                        linkPp.deleteLinkByProduct(productId);
                        LogHelper.AddLog("产品 " + productId + " 出库 ");
                    }
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                log.Error(ex.ToString());
                return false;
            }
        }

        public bool AddStatus(Status status)
        {
            return statusInterface.Add(status);
        }

    }
}
